import FormCalendarViewApi from "@/api/form/core/formCalendarView";
import {Util,GridMixin} from "@lowcode/jpaas-common-lib";
import SysAppAllMenu from "@/views/modules/system/core/SysAppAllMenu";
import SysMenuApi from "@/api/system/core/sysMenu";

import {I18Parse,CommonLang} from '@lowcode/jpaas-common-lib';
import jsLang from '../../../../../locales/lang/impl/form/jsLang';
let lang = jsLang;

let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang.jsLang.FormCalendarViewEdit2,langKey, text)
}

export default {
    props:["layerid","destroy"],
    mixins:[GridMixin],
    data() {
        return {
            activeKey:"columnConf",
            extJson:{
                //列头配置
                columnConf:{
                    //主键字段
                    idField:"",
                    startField:"",
                    endField:"",
                    timeFields:[{
                        startField:"",
                        endField:"",
                        color:""
                    }],
                    timeQuantum:false,
                    //月视图配置
                    monthConf:{
                        show:true,
                        monthTitle:"",
                    },
                    //周视图配置
                    weekConf:{
                        show:false,
                        weekColumns:[],
                    },
                    //日视图配置
                    dayConf:{
                        show:false,
                        dayColumns:[]
                    },
                },
                //按钮配置
                buttonConf:[],
                //js
                jsScript:"export default {\n" +
                    "  //"+i18n('defaultJsDesc1', '数据定义')+"\n" +
                    "  data(){\n" +
                    "    return {\n" +
                    "    }\n" +
                    "  },\n" +
                    "  //"+i18n('defaultJsDesc2', '初始化时加载')+"\n" +
                    "  created(){ \n" +
                    "  },\n" +
                    "  //"+i18n('defaultJsDesc3', '页面自定义函数')+"\n" +
                    "  methods:{\n" +
                    "  },\n" +
                    "  //"+i18n('defaultJsDesc5', '页面自定义观察函数')+"\n" +
                    "  watch:{\n" +
                    "  }\n" +
                    "}",
            },
            //所有字段
            fieldColumns:[],
            btnColumns:[
                {title: i18n('buttonName','按钮名称'), dataIndex: 'btnLabel', scopedSlots: {customRender: 'btnLabel'}},
                {title: i18n('buttonAlias','按钮别名'), dataIndex: 'btnName', scopedSlots: {customRender: 'btnName'}},
                {title: i18n('eventFunc','事件函数'), dataIndex: 'btnClick', scopedSlots: {customRender: 'btnClick'}},
                {title: i18n('typeButton','按钮类型'), dataIndex: 'btnType',  scopedSlots: {customRender: 'btnType'}}
            ],
            defBtns:[]
        }
    },
    methods: {
        onload_(values){
            //初始化
            if(values){
                if(values.extJson){
                    var extJson=JSON.parse(values.extJson)
                    if(extJson.columnConf){
                        if(!extJson.columnConf.timeFields){
                            extJson.columnConf.timeFields=[{
                                startField:extJson.columnConf.startField,
                                endField:extJson.columnConf.endField,
                                color:"#069f80"
                            }]
                        }
                        this.extJson.columnConf=extJson.columnConf;
                    }
                    if(extJson.buttonConf){
                        this.extJson.buttonConf=extJson.buttonConf;
                    }
                    if(extJson.jsScript){
                        this.extJson.jsScript=extJson.jsScript;
                    }
                }
                if(values.fieldColumns){
                    for (let i = 0; i < values.fieldColumns.length; i++) {
                        var fieldColumn=values.fieldColumns[i];
                        this.fieldColumns.push({
                            label:fieldColumn.fieldLabel,
                            value:fieldColumn.fieldName,
                            dataType:fieldColumn.dataType
                        })
                    }

                }
                //初始化默认按钮
                this.initDefBtns(values);
            }
        },
        initDefBtns(values){
            this.defBtns=[ {idx_: Util.randomId(),btnLabel:I18Parse.getI18Val(CommonLang,'rxList.add', '新增'),btnName:values.key+'_Add',btnType:'head',isDefault:true,btnClick:'onAdd'},
                {idx_: Util.randomId(),btnLabel:I18Parse.getI18Val(CommonLang,'rxList.editBtn','编辑'),btnName:values.key+'_Edit',btnType:'row',isDefault:true,btnClick:'onEdit'},
                {idx_: Util.randomId(),btnLabel:I18Parse.getI18Val(CommonLang,'rxList.delBtn','删除'),btnName:values.key+'_Remove',btnType:'row',isDefault:true,btnClick:'onRemove'},
                {idx_: Util.randomId(),btnLabel:I18Parse.getI18Val(CommonLang,'rxList.detailBtn','明细'),btnName:values.key+'_Detail',btnType:'row',isDefault:true,btnClick:'onDetail'}]
        },
        get(id) {
            return FormCalendarViewApi.getById(id,true);
        },
        save(values) {
            if(this.$refs.permissionConf){
                var permissionConf=this.$refs.permissionConf.getData();
                if(permissionConf && permissionConf.data){
                    values["permissionConf"]=JSON.stringify(permissionConf);
                }
            }
            return FormCalendarViewApi.save(values);
        },
        selectField(value){
            if(value){
               this.extJson.columnConf.monthConf.monthTitle+= '${'+value+'}';
            }
        },
        addBtn(){
            this.extJson.buttonConf.push({idx_: Util.randomId(),btnLabel:'',btnName:'',btnType:'row',isDefault:false,btnClick:''})
        },
        setDefBtns(){
            var defaultButtons= Util.listToTree(this.defBtns,'idx_');
            for(let btn of defaultButtons){
                let exist=this.isExists(btn.btnName);
                if(exist){
                    continue;
                }
                this.extJson.buttonConf.push(btn);
            }
        },
        //判断按钮是否存在。
        isExists(method){
            if(this.extJson.buttonConf !=undefined){
                for(let btn of this.extJson.buttonConf){
                    if(btn.btnName==method){
                        return true;
                    }
                }
            }
            return false;
        },
        removeBtn(){
            this.removeRows(this.extJson.buttonConf,this.$refs.table_Btn.selectedRowKeys);
            this.$refs.table_Btn.selectedRowKeys=[];
        },
        upBtn(){
            this.toUp( this.extJson.buttonConf,this.$refs.table_Btn.selectedRowKeys);
        },
        downBtn(){
            this.toDown(this.extJson.buttonConf,this.$refs.table_Btn.selectedRowKeys);
        },
        joinToolbarRs() {
            var table = this.$refs.table_Btn;
            var btns = table.getSelectedRows();
            if(!btns || btns.length==0){
                this.$message.error(i18n('authorization','请选择需要授权的按钮！'));
                return;
            }
            var conf = {
                component: SysAppAllMenu,
                title: i18n('menu','同步按钮至选择菜单'),
                curVm: this,
                widthHeight: ['400px', '550px']
            };
            Util.open(conf, function (action) {
                if (action != 'ok') {
                    return;
                }
                var menus = this.getSelectMenu();
                if(!menus || menus.length==0){
                    this.$message.error(i18n('selectTips','请选择菜单！'));
                    return;
                }
                SysMenuApi.syncMenuBtns({
                    menuId: menus[0].id,
                    btns: JSON.stringify(btns)
                }).then(res => {
                })
            });
        },
        //新增日期字段
        addTimeField(){
            this.extJson.columnConf.timeFields.push({
                startField:"",
                endField:"",
                background:"#069f80"
            })
        },
        //删除日期字段
        delTimeField(index){
            this.extJson.columnConf.timeFields.splice(index,1)
        }
    }
}